<template>
    <div class="admin">
        <!-- <panel @add="addUser" @search="search"></panel> -->
        <el-button type="danger" round @click="addAdmin" class="btn">新增</el-button>
        <adminList
            :adminList="adminList"
            class="list"
            @change="change($event); dialogFormVisible = true"
            @del="del"
        ></adminList>
        <!-- <el-pagination
            background
            layout="prev, pager, next"
            :total="total"
            :page-size="Number(page_num)"
            @current-change="handleCurrentChange"
            class="pagination"
        ></el-pagination> -->
        <adminUpdate
            :dialogFormVisible="dialogFormVisible"
            @closeDialog="dialogFormVisible = false"
            :toChangeRow="toChangeRow"
            @refresh="refresh"
        ></adminUpdate>
        <adminAdd
            :dialogFormVisible="showAddDialog"
            @closeDialog="showAddDialog=false"
            @refresh="refresh"
            :addTemp="{...addTemp}"
        ></adminAdd>
    </div>
</template>

<script>
import adminList from "@/components/admin/adminList";
import adminUpdate from "@/components/admin/adminUpdate";
import adminAdd from '../../components/admin/adminAdd';
export default {
    name: "Admin",
    data() {
        return {
            adminList: [],
            toChangeIndex: undefined,
            dialogFormVisible: false,
            toChangeRow: null,

            showAddDialog:false,
            addTemp:{
                account: "",
                password: ""
            }
        };
    },
    components: {
        adminList,
        adminUpdate,
        adminAdd
    },
    methods: {
        addAdmin: function() {
            this.showAddDialog=true;
        },
        change: function(index) {
            console.log(index);
            this.toChangeRow = {...this.adminList[index]};
        },
        del: async function(row) {
            let data = await this._axios
                .get("/ycwAdmin/admin/del?id=" + row.id)
                .then(data => data.data);
            console.log(data);
            if (data) {
                this.$alert(`${data.msg}`, {
                    confirmButtonText: "确定",
                    callback: () => {
                        this.$message({
                            type: "warning",
                            message: `${data.msg}`
                        });
                        this.refresh();
                    }
                });
            }
        },
        refresh: async function() {
            let data = await this._axios
                .get("/ycwAdmin/admin/get")
                .then(data => data.data.data);
            console.log(data, 123);
            // this.page_now = data.page_now;
            // this.total = data.page_total * data.page_num;
            this.adminList = data;
            // this.page_num = data.page_num;
        }
    },
    mounted: async function() {
        this.refresh();
    }
};
</script>

<style lang="scss" scoped>
// .pagination {
//     margin-top: 10px;
// }
.btn{
    position: relative;
    left: -50%;
    transform: translateX(50%);
    margin-bottom: 20px;
}
</style>